import { Component, ComponentType } from "../Component";
import { any } from "../../core/logic";
import { Buffer } from "../../gpu/types/Buffer";
import { Engine } from "../../Engine";
/**
 * @memberof w.light
 */
class PunctualLight extends Component {
  constructor(opts = {}, share) {
    opts.type = any(opts.type, ComponentType.PunctualLight);
    super(opts, share);
    this.lightCameraArray = [];
    this._punctualLights = [];
    this._count = any(opts.count, 0);
    this._initdefault(); //默认给0束精准光线。
    this.autoUpdate = true;
    this.hash=""
    this.update();
  }
  _initdefault() {
    const itemSize=Engine.instance.shadowEnable?32:16;//是否开启阴影。
    let lightGPUParameter = new Buffer({
      id: "punctualLight",
      name: "punctualLight",
      usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
      visibility: GPUShaderStage.FRAGMENT,
      type: "uniform",
      size: itemSize*10*4,
      data: new Float32Array(itemSize * 10),
    });
    this.addParam("punctualLight", lightGPUParameter);
    lightGPUParameter.state = true;
    //光源数量
    let lightCountParameter = new Buffer({
      id: "punctualCount",
      name: "punctualCount",
      usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
      visibility: GPUShaderStage.FRAGMENT,
      type: "uniform",
      size: 4,
      data: new Uint32Array([0]),
    });
    this.addParam("punctualCount", lightCountParameter);
    lightCountParameter.state = true;
  }
  get count() {
    return this._count;
  }
  set count(v) {
    this._count = v;
    this.addParam("punctualCount");
    return this;
  }
  get punctualLights() {
    return this._punctualLights;
  }
  set punctualLights(v) {
    this._punctualLights = v;
    this.addParam("punctualLight");
    this.count = v ? v.length : 0;
    this.update(this.event.mode);
    return this;
  }
  updateState() {
    this.setState(true);
  }

  toGPU() {
    let lightGPUParameter = this.getParam("punctualLight");
    let lightCountParameter = this.getParam("punctualCount");
    if (!lightGPUParameter.state) {
      let punctualLightData 
      if(Engine.instance.shadowEnable){
        punctualLightData = new Float32Array(32 * 10)
      }else{
        punctualLightData = new Float32Array(16 * 10)
      }
      let castCount=0;
      this.lightCameraArray = [];
      this.pointShadow = [];
      for(let index=0;index<this.punctualLights.length;index++){
        const light=this.punctualLights[index];
        if(Engine.instance.shadowEnable){
          punctualLightData.set(light.getLightBuffer(), 32 * index);
        }else{
          punctualLightData.set(light.getLightBuffer(), 16 * index);
        }
        if (light.cast) {
          castCount++
          if(light.lightType===1){
             this.pointShadow.push(light.shadowBuffer)
          }else{
            this.lightCameraArray.push(light.shadowBuffer)
           
          }
    
        }
      };
      if (Engine.instance.shadowEnable) {//灯光投影下的数据
        // 灯光阴影的数量
        this.addConfigParam("lightCastCount", castCount);
      }
      lightGPUParameter.buffer(punctualLightData);
    }
    if (!lightCountParameter.state) {
      lightCountParameter.buffer(new Uint32Array([this.count]));
      lightCountParameter.state = true;
    }
  }
}

export { PunctualLight };
